import logging
from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from datetime import date, datetime

from horseanalysis.lib.base import render
from horseanalysis.controllers.application import ApplicationController
from horseanalysis.model.race import Race
from horseanalysis.model.race_batch import RaceBatch
from horseanalysis.model.betting_scheme import BettingScheme
from horseanalysis.model.probability_sets.quinella_probability_result import QuinellaProbabilityResult
from horseanalysis.model.probability_sets.quinella_place_probability_result import QuinellaPlaceProbabilityResult
from horseanalysis.model.probability_sets.place_probability_result import PlaceProbabilityResult

log = logging.getLogger(__name__)

FROM_EXPECTED_RETURN = 0.9

class SchemesController(ApplicationController):

  def index(self):
    return render('/schemes/index.html')
  
  def show(self, id):
    c.race_batch = RaceBatch.get_batch(id)
    c.race = c.race_batch.race
    c.schemes = BettingScheme.get_all_schemes()
    c.place_prob_sets = {}
    c.quinella_prob_sets = {}
    c.qplace_prob_sets = {}
    
    for scheme in c.schemes:
      c.place_prob_sets[scheme.name]=PlaceProbabilityResult.get_by_expected_return(c.race_batch, FROM_EXPECTED_RETURN)
      c.quinella_prob_sets[scheme.name]=QuinellaProbabilityResult.get_by_expected_return(c.race_batch, FROM_EXPECTED_RETURN)
      c.qplace_prob_sets[scheme.name]=QuinellaPlaceProbabilityResult.get_by_expected_return(c.race_batch, FROM_EXPECTED_RETURN)
    
    return render('/schemes/show.html')